import React, {useState, useEffect} from "react";
import {
  BUFFER_STROKE_CALCULATION,
} from "../../uitl/constant-filename";
import {Button, Descriptions, Input} from "antd";

/**
 * 缓冲器行程
 * @constructor
 */
function BufferStrokeCalculation() {

  const [W, setW] = useState(null);
  const [V, setV] = useState(null);
  const [K, setK] = useState(null);
  const [N, setN] = useState(null);
  const [ZS_t, setZS_t] = useState(null);
  const [QS_t, setQS_t] = useState(null);
  const [S, setS] = useState(null);
  const [QS, setQS] = useState(null);

  const [readOnly, setReadOnly] = useState(false);

  useEffect(() => {
    /* command argv */
    window.electronAPI.commandArgv((_event, content) => {
      console.log('content:', content);
      const objs = JSON.parse(content);
      if (objs.length > 0) {
        setReadOnly(true);
        objs.forEach(obj => {
          if (obj.code === 'QLJ_W') {
            // 设计最大着陆重量
            obj.value !== undefined ? setW(obj.value) : setW(8000);
          }
          else if (obj.code === 'ZLSD') {
            // 下沉速度
            obj.value !== undefined ? setV(obj.value) : setV(3.05);
          }
          else if (obj.code === 'QLJ_K') {
            // 着陆升重比L／G
            obj.value !== undefined ? setK(obj.value) : setK(1);
          }
          else if (obj.code === 'overload') {
            // 机身过载
            obj.value !== undefined ? setN(obj.value) : setN(3);
          }
          else if (obj.code === 'LT_St') {
            // 主起落架N倍停机载荷下轮胎变形
            obj.value !== undefined ? setZS_t(obj.value) : setZS_t(0.12);
          }
          else if (obj.code === 'QLT_St') {
            // 前起落架N倍停机载荷下轮胎变形
            obj.value !== undefined ? setQS_t(obj.value) : setQS_t(0.055);
          }
        });
      }
      else {
        setReadOnly(false);

        setW(8000)
        setV(3.05)
        setK(1);
        setN(3);
        setZS_t(0.12);
        setQS_t(0.055);
      }
    });
  }, []);

  function onChange(e) {
    const target = e.target;
    const name = target.name;
    const value = target.value;

    if (name === 'W') {
      setW(value);
    }
    else if (name === 'V') {
      setV(value);
    }
    else if (name === 'K') {
      setK(value);
    }
    else if (name === 'N') {
      setN(value);
    }
    else if (name === 'ZS_t') {
      setZS_t(value);
    }
    else if (name === 'QS_t') {
      setQS_t(value);
    }
  }

  function calculate() {const g=9.81
    const S = (V * V / (2 * g) - (0.47 * N + K - 1) * ZS_t) / (0.8 * N + K - 1);
    const QS = (V * V / (2 * g) - (0.47 * N + K - 1) * QS_t) / (0.8 * N + K - 1);
    setS(S);
    setQS(QS);

    const obj = [
      {
        id: '标识',
        name: '名称',
        code: '代号',
        value: '值'
      },
      {
        id: 205,
        name: '主起落架缓冲器行程',
        code: 'ZHCQ_S',
        value: S
      },
      {
        id: 278,
        name: '前起落架缓冲器行程',
        code: 'QHCQ_S',
        value: QS
      }
    ];

    window.electronAPI.createOutputFile(obj, BUFFER_STROKE_CALCULATION);
  }

  return <div className={"tyre-selection"}>
    <div className={"sub-title"}>输入参数</div>
    <Descriptions bordered column={4}>
      <Descriptions.Item label="飞机的最大总重(kg)" span={2}>
        <Input value={W} name={"W"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="下沉速度(m/s)" span={2}>
        <Input value={V} name={"V"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="升重比" span={2}>
        <Input value={K} name={"K"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="主起落架轮胎变形量(m)" span={2}>
        <Input value={ZS_t} name={"ZS_t"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="前起落架轮胎变形量(m)" span={2}>
        <Input value={QS_t} name={"QS_t"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
    </Descriptions>

    <div className={"sub-title"}>
      计算结果
      <Button
        type={"text"}
        onClick={calculate}
        style={{ color: "#1677FF" }}
      >
        计算
      </Button>
    </div>
    <Descriptions bordered column={2}>
      <Descriptions.Item label="主起落架机轮的行程(m)" span={2}>{S}</Descriptions.Item>
      <Descriptions.Item label="前起落架机轮的行程(m)" span={2}>{QS}</Descriptions.Item>
    </Descriptions>
  </div>;

}

export default BufferStrokeCalculation;
